'use strict';

const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ErrorOverlayPlugin = require('error-overlay-webpack-plugin');
const utils = require('../utils');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const LoadablePlugin = require('@loadable/webpack-plugin');
/**
 *
 * @param {object} data
 * @param {*} data.buildPath
 * @param {*} data.mainPath
 * @param {*} data.moduleName
 * @param {*} data.gitLoacl
 * @param {*} data.ctx
 */
module.exports = function (data, config) {
  return {
    entry: {
      main: utils.resolve('template/_app.tsx'),
    },
    output: {
      publicPath: `/static/${data.name}/${data.moduleName}/client`,
      path: path.join(data.buildPath, 'client'),
      filename: '[name].js',
      chunkFilename: '[name].js',
    },
    mode: 'development',
    devtool: 'cheap-module-source-map',
    stats: { children: false },
    resolve: utils.getResolve(data, config),
    resolveLoader: utils.getResolveLoader(data),
    externals: {
      vue: 'Vue',
    },
    module: {
      noParse: /^(vue|vue-router|vuex|vuex-router-sync)$/,
      rules: [
        utils.getVueLoader(),
        utils.getImageLoader(),
        utils.getLessLoader(true, config),
        utils.getCssLoader(true, config),
        utils.getMediaLoader(),
        utils.getFontsLoader(),
        utils.getSvgLoader(),
        ...utils.getTsLoader(data, config),
      ],
    },
    plugins: [
      new CleanWebpackPlugin(),
      new VueLoaderPlugin(),
      new ErrorOverlayPlugin(),
      new HtmlWebpackPlugin({
        template: utils.resolve('views/dev-index.html'),
        filename: 'index.html',
        inject: 'body',
        title: 'aaaaa',
      }),
      new webpack.LoaderOptionsPlugin({
        debug: true,
        minimize: false,
        options: {
          context: data.mainPath,
        },
      }),
      new LoadablePlugin({
        filename: 'loadable-stats.dev.json',
        writeToDisk: {
          filename: data.buildPath,
        },
      }),
      new webpack.LoaderOptionsPlugin({
        debug: true,
        minimize: false,
        options: {
          context: data.mainPath,
        },
      }),
    ],
  };
};
